package edu.scau.web.pos;

import java.util.Date;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.pos.domain.Orders;
import edu.scau.pos.service.OrdersService;
import edu.scau.pos.vo.OrdersVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Controller
 */
//@Hidden
@RestController
@RequestMapping("/orders")
@Tag(name = "订单CRUD接口")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Operation(
        summary = "查找所有订单",
        description = "查找所有未删除的信息,访问权限：admin,cashier",
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class))),
            @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class)))
        }
    )
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<OrdersVo> ordersList = ordersService.listAll();
        return AjaxResult.success(ordersList);
    }

    @Operation(
            summary = "根据ID查找orders,",
            description = "id查找未删除的信息,访问权限：admin,cashier",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Orders.class)))
            }
    )
    @Hidden
    @GetMapping("/listAllPropertyById")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult listAllProperty(@RequestParam("orderId") String orderId) {
        Orders orders= ordersService.findByOrderId(orderId);
        return AjaxResult.success(orders);
    }

    @Operation(
        summary = "分页查找",
        description = "分页查找所有未删除的信息,不支持条件查询,访问权限：admin,cashier",
        parameters = {
            @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
            @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class))), // 返回 PageInfo 对象
            @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) // 返回 AjaxResult 对象
        }
    )
    @GetMapping("/listByPage")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult listByPage(
        @RequestParam("pageNum") Integer pageNum,
        @RequestParam("pageSize") Integer pageSize
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrdersVo> ordersList = ordersService.listAll();
        PageInfo<OrdersVo> pageInfo = new PageInfo<>(ordersList);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
            summary = "条件查询订单",
            description = "条件查询订单，查找所有未删除的信息,访问权限：admin,cashier",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class))),
                    @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class)))
            }
    )
    @PostMapping("/listByOrders")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult listByOrders(@org.springframework.web.bind.annotation.RequestBody OrdersVo ordersVo) {
        List<OrdersVo> ordersList = ordersService.listByOrders(ordersVo);
        return AjaxResult.success(ordersList);
    }

    @Operation(
            summary = "根据订单状态订单",
            description = "根据订单状态订单，查找所有未删除的信息,访问权限：admin,cashier",
            parameters = {
                    @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
                    @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int")),
                    @Parameter(name = "status", description = "2：成功订单；3：挂单订单", required = true, schema = @Schema(type = "int"))
            }
    )
    @PostMapping("/listByOrdersOfStatus")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult listByOrdersOfStatus(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("status") int status
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrdersVo> ordersList = ordersService.listByOrdersOfStatus(status);
        PageInfo<OrdersVo> pageInfo = new PageInfo<>(ordersList);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
        summary = "根据ID查找订单",
        description = "根据ID查找信息,访问权限：admin,cashier",
        parameters = {
            @Parameter(name = "ordersId", description = "ID", required = true, schema = @Schema(type = "Long"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = OrdersVo.class))),
            @ApiResponse(responseCode = "204", description = "不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/findOrdersById")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult findOrdersById(@RequestParam("ordersId") String ordersId) {
        OrdersVo orders = ordersService.findOrdersByOrderId(ordersId);
        if (orders != null) {
            return AjaxResult.success(orders);
        } else {
            return AjaxResult.success("不存在或者已删除！", HttpStatus.NO_CONTENT);
        }
    }

    @Operation(
        summary = "更新订单信息",
        description = "根据ID更新信息,访问权限：admin,cashier",
        requestBody = @RequestBody(
            description = "更新条件,id是必须的",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = OrdersVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/update")
    @Hidden
    //orderVo数据不完整，不允许前端无业务情况修改已存在订单
    public AjaxResult updateOrders(@Validated  @org.springframework.web.bind.annotation.RequestBody OrdersVo orders) {
       try {
           int res = ordersService.updateOrdersByOrdersVo(orders);
           return res > 0 ? AjaxResult.success(HttpStatus.SUCCESS) : AjaxResult.error("更新失败");
       }catch (IllegalArgumentException e){
           return AjaxResult.error(e.getMessage());
       }
    }

    @Operation(
        summary = "插入订单",
        description = "插入信息,访问权限：admin,cashier",
        requestBody = @RequestBody(
            description = "插入条件",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = OrdersVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/insert")
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    @Hidden
    public AjaxResult insertOrders(@Validated @org.springframework.web.bind.annotation.RequestBody OrdersVo orders) {
        try {
            int res = ordersService.insertOrdersByOrrdersVo(orders);
            return res > 0 ? AjaxResult.success(200) : AjaxResult.error("插入失败！");
        }catch (IllegalArgumentException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(
        summary = "根据ID删除订单",
        description = "根据ID删除信息,访问权限：admin,cashier",
        parameters = {
            @Parameter(name = "ordersId", description = "ID", required = true, schema = @Schema(type = "Long"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @DeleteMapping("/delete")
    @Hidden
    @PreAuthorize("hasAnyAuthority('admin','cashier')")
    public AjaxResult delete(@RequestParam("ordersId") String ordersId) {
        int res = ordersService.deleteOrdersById(ordersId);
        return res > 0 ? AjaxResult.success("删除成功", 200) : AjaxResult.error("删除失败");
    }
}
